<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/25
 * Time: 10:46
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\ShortVideoModel;
use App\Http\Model\Mysql\ShortCollectModel;
use App\Http\Model\MongoDB\ShopTemplate;

class ShortVideoService extends BaseService
{
    /**
     * 短视频列表
     * @param int $page
     * @return array
     */
    public function videoList(int $page = 1)
    {
        $count  =   ShortVideoModel::where(['is_delete' => ShortVideoModel::NOT_DELETE])->count();
        $list   =   ShortVideoModel::with([
                        'good'  =>  function ($query) {
                            $query->with([
                                'product'   =>  function ($fetch) {
                                    $fetch->select(['id', 'unique_id', 'name', 'sell_price', 'stock', 'cover_pic']);
                                }
                            ])->select(['id', 'good_id', 'video_id']);
                        }
                    ])->where(['is_delete' => ShortVideoModel::NOT_DELETE])->limit(self::PAGE_LIMIT)->offset(($page - 1) * self::PAGE_LIMIT)
                    ->select(['id', 'short_name', 'short_cover', 'cart_times', 'click_times', 'collect_times', 'play_times', 'video_url'])->get()->each(function (&$item) {

                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'current'=> $page,
                'pages' =>  ceil($count / self::PAGE_LIMIT),
                'list'  =>  $list->toArray()
            ]];
        }
    }

    /**
     * 短视频页面
     * @return array
     */
    public function videoPage()
    {
        $page_info  =   ShopTemplate::where(['_id' => '58ea42e0-067e-4b26-8b34-26b03dcc664b'])->first();
        if (empty($page_info)) {
            return [false, []];
        }
        return [true, $page_info];
    }

    /**
     * 短视频详情
     * @param int $video_id
     * @return array
     */
    public function videoDetail(int $video_id)
    {
       $detail  =   ShortVideoModel::with([
                        'good'  =>  function ($query) {
                            $query->with([
                                'product'   =>  function ($fetch) {
                                    $fetch->select(['id', 'unique_id', 'name', 'sell_price', 'stock', 'cover_pic']);
                                }
                            ])->select(['id', 'good_id', 'video_id']);
                        }
                    ])->where(['id' => $video_id, 'is_delete' => ShortVideoModel::NOT_DELETE])->select(['id', 'short_name', 'short_cover', 'cart_times', 'click_times', 'collect_times', 'play_times', 'video_url'])->first();

       return [true, $detail];

    }

    /**
     * 点击次数
     * @param int $video_id
     * @return array
     */
    public function videoClick(int $video_id)
    {
        $result =   ShortVideoModel::where(['id' => $video_id])->increment('click_times');
        return [true, $result];
    }

    /**
     * 播放次数
     * @param int $video_id
     * @return array
     */
    public function videoPlay(int $video_id)
    {
        $result =   ShortVideoModel::where(['id' => $video_id])->increment('play_times');
        return [true, $result];
    }

    /**
     * 点赞次数
     * @param int $video_id
     * @return array
     */
    public function videoCart(int $video_id)
    {
        $result =   ShortVideoModel::where(['id' => $video_id])->increment('cart_times');
        return [true, $result];
    }

    /**
     * 分享次数
     * @param int $video_id
     * @return array
     */
    public function videoCollect(int $video_id)
    {
        $user_info  =   request()->post('api_user');
        if (ShortCollectModel::where(['video' => $video_id, 'user_id' => $user_info['user_id']])->exists()) {
            return [false, '已经收藏过'];
        }
        $result =   ShortCollectModel::create(['video' => $video_id, 'user_id' => $user_info['user_id']]);
        if ($result) {
            ShortVideoModel::where(['id' => $video_id])->increment('collect_times');
            return [true, '收藏成功'];
        } else {
            return [false, '收藏失败'];
        }
    }
}
